﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Diagnostics;
using CommonClass;
using CommonBase;

namespace SurveillanceLib
{
    /// <summary>
    /// 局部公共静态方法与常量
    /// </summary>
    public static class SurveillanceUtilities
    {
        #region 常量

        public const string ALREADY_ON_THE_WALL = "大屏已上墙显示";
        public const string NOT_ON_THE_WALL = "大屏未上墙";
        public const string STOP_REALPLAY_FAILURE = "停止视频操作失败";

        /// <summary>
        /// 最后操作信息显示格式
        /// </summary>
        public const string ERR_INFO_FORMAT_STYLE = "代码: errcode;\n描述: errmSG.";

        /// <summary>
        /// 最多查询文件个数
        /// </summary>
        public const int FILES_MAXCOUNT = 1024;

        #region readonly

        /// <summary>
        /// 区域监控图片文件保存默认路径
        /// </summary>
        public static readonly string USERCONFIG_REGIONSURVEILLANCEPATH_DEFAULT = Path.Combine(
                Environment.CurrentDirectory, "RegionSurveillance");

        /// <summary>
        /// 远程录像回放文件下载保存默认路径
        /// </summary>
        public static readonly string USERCONFIG_DOWNLOADVIDEOPATH_DEFAULT = Path.Combine(
                Environment.CurrentDirectory, "DownloadVideo");

        /// <summary>
        /// 流数据录像文件保存默认路径
        /// </summary>
        public static readonly string USERCONFIG_RECORDVIDEOPATH_DEFAULT = Path.Combine(
                Environment.CurrentDirectory, "RecordVideo");

        /// <summary>
        /// 远程录像回放文件转换保存默认路径
        /// </summary>
        public static readonly string USERCONFIG_CONVERTVIDEOPATH_DEFAULT = Path.Combine(
                Environment.CurrentDirectory, "ConvertVideo");

        /// <summary>
        /// 视频窗口截图保存默认路径
        /// </summary>
        public static readonly string USERCONFIG_SCREENSHOTPATH_DEFAULT = Path.Combine(
                Environment.CurrentDirectory, "ScreenShot");

        #endregion

        #region INI

        /// <summary>
        /// ini 文件[UserConfig]
        /// </summary>
        public const string INI_SECTIONNAME_USERCONFIG = "UserConfig";

        /// <summary>
        /// ini 文件[UserConfig] DownloadVideoPath
        /// </summary>
        public const string INI_KEYNAME_USERCONFIG_DOWNLOADVIDEOPATH = "DownloadVideoPath";

        /// <summary>
        /// ini 文件[UserConfig] RecordVideoPath
        /// </summary>
        public const string INI_KEYNAME_USERCONFIG_RECORDVIDEOPATH = "RecordVideoPath";

        /// <summary>
        /// ini 文件[UserConfig] ConvertVideoPath
        /// </summary>
        public const string INI_KEYNAME_USERCONFIG_CONVERTVIDEOPATH = "ConvertVideoPath";

        /// <summary>
        /// ini 文件[UserConfig] ScreenShotPath
        /// </summary>
        public const string INI_KEYNAME_USERCONFIG_SCREENSHOTPATH = "ScreenShotPath";

        #endregion

        #endregion

        #region 属性

        /// <summary>
        /// 获取主界面中的预览大屏模块，用于控制按钮的显示
        /// </summary>
        public static Panel PanelAllWall { get; set; }

        /// <summary>
        /// 录像文件下载路径
        /// </summary>
        public static string DownloadVideoPath { get; set; }

        /// <summary>
        /// 录像路径
        /// </summary>
        public static string RecordVideoPath { get; set; }

        /// <summary>
        /// 转换路径
        /// </summary>
        public static string ConvertVideoPath { get; set; }

        /// <summary>
        /// 截图路径
        /// </summary>
        public static string ScreenShotPath { get; set; }

        #endregion

        #region 方法

        /// <summary>
        /// 停止该窗口的视频后在另一窗口中播放
        /// </summary>
        /// <param name="panelStop">要停止的 Panel</param>
        /// <param name="panelPlay">要播放的 Panel</param>
        public static void StopAndPlay(Panel panelStop, Panel panelPlay)
        {
            try
            {
                int outChannel;
                if (panelStop.Tag != null && panelStop.Tag is TagModel &&
                    (panelStop.Tag as TagModel).Driver_Instance != null &&
                    !string.IsNullOrWhiteSpace((panelStop.Tag as TagModel).Driver_LoginID) &&
                    !string.IsNullOrWhiteSpace((panelStop.Tag as TagModel).Panel_SelectedChannel) &&
                    int.TryParse((panelStop.Tag as TagModel).Panel_SelectedChannel, out outChannel))
                {
                    TagModel panelStopTag = panelStop.Tag as TagModel;

                    // a. 先停止小窗口中的视频
                    if (!string.IsNullOrWhiteSpace((panelStop.Tag as TagModel).Panel_RealPlayHandle))
                    {
                        if (panelStopTag.Driver_Instance.StopRealPlay(panelStopTag.Panel_RealPlayHandle)) // 停止实时监控，需要传递实时监控句柄
                            panelStopTag.Panel_RealPlayHandle = string.Empty;
                        else
                        {
                            MessageBox.Show(STOP_REALPLAY_FAILURE, ConstantString.MSG_COMPANY);
                            return;
                        }
                    }

                    // b. 再播放大窗口中的视频
                    string pRealPlayHandle = panelStopTag.Driver_Instance.RealPlay(panelStopTag.Driver_LoginID,
                        outChannel, panelPlay.Handle); // 只取数据不播放

                    // c. 得到最新的 RealPlayHandle
                    Utilities.CopyObject(panelStopTag, panelPlay.Tag);
                    (panelPlay.Tag as TagModel).Panel_RealPlayHandle = pRealPlayHandle;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 根据 <see cref="tagModel"/> 设置 <see cref="panelSetTooltip"/> 的 Tooltip
        /// </summary>
        /// <param name="panelSetTooltip">需要设置 Tooltip 的分屏</param>
        /// <param name="tagModel">相关信息，Tooltip 的源</param>
        /// <param name="toolTip">ToolTip 实例</param>
        /// <param name="isShowLabelAndColor">是否在 <see cref="tagModel"/> 上显示标签与背景色</param>
        /// <param name="bgColor">背景色</param>
        /// <param name="otherMessage">Panel 上显示的附加信息</param>
        public static void SetPanelTooltip(Panel panelSetTooltip,
            TagModel tagModel, ToolTip toolTip, bool isShowLabelAndColor, Color bgColor,
            string otherMessage = null)
        {
            try
            {
                if (tagModel == null || toolTip == null)
                    return;

                int outChannel;
                if (!string.IsNullOrWhiteSpace(tagModel.Panel_SelectedChannel) &&
                    int.TryParse(tagModel.Panel_SelectedChannel, out outChannel)) // 修改背景色、显示信息等
                {
                    string lbInfor;
                    if (bgColor == Color.LimeGreen) // 正常情况
                        lbInfor = string.Format("{0}\n通道{1}", tagModel.Driver_IP, outChannel + 1); // Label 显示信息
                    else
                        lbInfor = otherMessage;

                    Label lbDevice = new Label()
                    {
                        Text = lbInfor,
                        Height = 25,
                    };
                    // 设置位置
                    lbDevice.Location = new Point((panelSetTooltip.Width - lbDevice.Width) / 2,
                        (panelSetTooltip.Height - lbDevice.Height) / 2);
                    // 设置 Tooltip
                    toolTip.SetToolTip(panelSetTooltip, lbInfor);
                    toolTip.SetToolTip(lbDevice, lbInfor);

                    if (isShowLabelAndColor) // 显示 Label 与背景色
                    {
                        panelSetTooltip.Controls.Add(lbDevice);
                        panelSetTooltip.BackColor = bgColor;
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
